package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbGroup;
import com.bwie.service.TbGroupService;
import com.bwie.mapper.TbGroupMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
* @author 叶雨洁
* @description 针对表【tb_group】的数据库操作Service实现
* @createDate 2025-07-27 19:14:38
*/
@Service
public class TbGroupServiceImpl extends ServiceImpl<TbGroupMapper, TbGroup>
    implements TbGroupService{

    @Autowired
    TbGroupMapper tbGroupMapper;

    @Autowired
    HttpServletRequest request;
    @Override
    public R TeamActivePageList(PageInfoVo pageInfoVo) {

        Page<TbGroup> tbGroupPage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        QueryWrapper<TbGroup> queryWrapper = new QueryWrapper<>();
        if(pageInfoVo.getTypeId()!=null){
            queryWrapper.lambda().eq(TbGroup::getGroupStatus,pageInfoVo.getTypeId());
        }

        Page<TbGroup> tbGroupPage1 = tbGroupMapper.selectPage(tbGroupPage, queryWrapper);
        return R.OK(tbGroupPage1);
    }

    @Override
    public R instr(TbGroup group) {

        tbGroupMapper.insert(group);
        return R.OK();
    }

    @Override
    public R addGroup(TbGroup tbGroup) {

        try {
            TbGroup tbGroup1 = tbGroupMapper.selectOne(
                    new QueryWrapper<TbGroup>().lambda().eq(TbGroup::getGroupId, tbGroup.getJoinId())
            );
            if(tbGroup1!=null){
                return R.ERROR(500,"已有该拼团");
            }
            //开始时间默认的是今天
            //结束时间是一周之后
//        LocalDateTime localDateTime = LocalDateTime.ofInstant(tbGroup.getBeginTime().toInstant(), ZoneId.systemDefault());
//        LocalDateTime localDateTime1 = localDateTime.plusWeeks(1);
//        tbGroup.setBeginTime(new Date());
//        Instant instant = localDateTime1.atZone(ZoneId.systemDefault()).toInstant();
//        Date from = Date.from(instant);
//        tbGroup.setEndTime(from);

          tbGroupMapper.insert(tbGroup);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return R.OK();
    }

    @Override
    public R myGroup() {
        String token = request.getHeader("token");
        Claims body = Jwts.parser().setSigningKey("bwie").parseClaimsJws(token).getBody();
        Integer userId = body.get("userId", Integer.class);
        List<TbGroup> mygroupList= tbGroupMapper.myGroupList(userId);
        System.out.println(mygroupList);
        return R.OK(mygroupList);
    }
}




